<?php
// +----------------------------------------------------------------------
// | Description: 基础类，无需验证权限。
// +----------------------------------------------------------------------
// | Author: linchuangbin <linchuangbin@honraytech.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\model\Admin;
use app\common\model\SystemConfig;
use app\admin\service\AdminService;
use app\admin\validate\AdminValidate;
use common\controller\BaseController;
use common\exception\ExceptionEmun;
use common\exception\SystemException;
use common\model\ES\base\ESTest;
use think\captcha\Captcha;
use think\Container;

class Base extends BaseController
{
    public function login()
    {
        $AdminService = AdminService::instance();
        $param = input_extract_rule([
            'username' => '',
            'password' => '',
            'isRemember' => '',
            'captcha' => '',
        ], 'post');
        AdminValidate::instance()->runCheck($param, '', __FUNCTION__);
        $username = $param['username'];
        $password = $param['password'];
        $isRemember = !empty($param['isRemember']) ? $param['isRemember'] : '';
        $data = $AdminService->login($username, $password, $isRemember);
        return $this->json_responce($data);
    }

    public function relogin()
    {
        $AdminService = AdminService::instance();
        $param = $this->param;
        AdminValidate::instance()->runCheck($param, '', __FUNCTION__);
        $data = decrypt($param['rememberKey']);
        $username = $data['username'];
        $password = $data['password'];
        $data = $AdminService->login($username, $password, '', true, true);
        return $this->json_responce($data);
    }

    public function logout()
    {
        $param = $this->param;
        cache('Auth_' . $param['authkey'], null);
        return $this->json_responce( '退出成功');
    }

    public function getConfigs()
    {
        $SystemConfigModel = SystemConfig::instance();
        $systemConfig = cache('DB_CONFIG_DATA');
        if (!$systemConfig) {
            //获取所有系统配置
            $systemConfig = $SystemConfigModel->getDataList();
            cache('DB_CONFIG_DATA', null);
            cache('DB_CONFIG_DATA', $systemConfig, 36000); //缓存配置
        }
        return $this->json_responce( $systemConfig);
    }

    public function getVerify()
    {
        return $this->captcha();
    }

    public function setInfo()
    {
        $Admin = Admin::instance();
        $param = $this->param;
        AdminValidate::instance()->runCheck($param, '', __FUNCTION__);
        $old_pwd = $param['old_pwd'];
        $new_pwd = $param['new_pwd'];
        $auth_key = $param['auth_key'];
        $data = $Admin->setInfo($auth_key, $old_pwd, $new_pwd);
        return $this->json_responce($data);
    }

    public function captcha()
    {
        $config = [
            'length' => 4,
        ];
        $id = AdminValidate::instance()->getCaptchaId();
        $captcha = new Captcha($config);
        return $captcha->entry($id);
    }

    public function checkCaptcha()
    {
        $captcha = input('get.captcha');
        $id = AdminValidate::instance()->getCaptchaId();
        return $this->json_responce( captcha_check($captcha, $id));
    }


    // miss 路由：处理没有匹配到的路由规则
    public function miss()
    {
        ExceptionEmun::throwException(SystemException::INVALID_ROUTE);
    }

    // miss 路由：处理没有匹配到的路由规则
    public function testInsert()
    {
        $ESTest_obj = ESTest::instance();
        $body = array(
            [
                'content' => '美国留给伊拉克的是个烂摊子吗',
                'name' => '美国留给伊拉克',
            ],
            [
                'content' => '公安部：各地校车将享最高路权',
                'name' => '公安部各地校车',
            ],
            [
                'content' => '中韩渔警冲突调查：韩警平均每天扣1艘中国渔船',
                'name' => '中韩渔警冲突',
            ],
            [
                'content' => '中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首',
                'name' => '中国驻洛杉矶领事馆',
            ],
        );
        $ESTest_obj->deleteAll();
        $res = $ESTest_obj->insert($body);
        return $this->json_responce();
    }

    public function testSearch()
    {
        $ESTest_obj = ESTest::instance();
        $res = $ESTest_obj->search('name','中国');
        return $this->json_responce($res);
    }
}
 